package com.example.demo.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LockAop {
    @Pointcut("@annotation(com.example.demo.annotate.Lock)")
    public void LockAop() {

    }

    /**
     * 加锁时，最好用try捕捉异常，有异常时及时关闭锁
     * 注：会导致Request型参数验证失效，但DTO型参数验证正常
     */
    @Around("LockAop()")
    public Object around(ProceedingJoinPoint joinPoint) {
        System.out.println("lock start...");

        Object obj;
        try {
            obj = joinPoint.proceed();
        }
        catch (Throwable e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        finally {
            System.out.println("lock end...");
        }

        return obj;
    }
}
